package com.gitee.wsl.android.ui.ext

import android.content.Context
import android.graphics.Color
import android.graphics.Paint
import com.google.android.material.shape.CornerFamily
import com.google.android.material.shape.CutCornerTreatment
import com.google.android.material.shape.MarkerEdgeTreatment
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.OffsetEdgeTreatment
import com.google.android.material.shape.RelativeCornerSize
import com.google.android.material.shape.RoundedCornerTreatment
import com.google.android.material.shape.ShapeAppearanceModel
import com.google.android.material.shape.Shapeable
import com.google.android.material.shape.TriangleEdgeTreatment

object MaterialShapeDrawableExt {
    //圆角效果
    fun round(value:Float,tintColor:Int=Color.GRAY): MaterialShapeDrawable {
        val shapeModel = ShapeAppearanceModel.builder()
            .setAllCorners(RoundedCornerTreatment())
            .setAllCornerSizes(value)
            .build()
        return MaterialShapeDrawable(shapeModel).apply {
            setTint(tintColor)
            paintStyle = Paint.Style.FILL
        }
    }

    //切角效果
    fun cut(value:Float,tintColor:Int=Color.GRAY): MaterialShapeDrawable {
        val shapeModel = ShapeAppearanceModel.builder()
            .setAllCorners(CutCornerTreatment())
            .setAllCornerSizes(value)
            .build()
        return MaterialShapeDrawable(shapeModel).apply {
            setTint(tintColor)
            paintStyle = Paint.Style.FILL
        }
    }

    //标注效果
    fun roundedCorner(round:Float,edgeRound:Float,strokeWidth1:Float,tintColor:Int=Color.GRAY): MaterialShapeDrawable {
        val shapeModel = ShapeAppearanceModel.builder()
            .setAllCorners(RoundedCornerTreatment())
            .setAllCornerSizes(round)
            .setAllEdges(MarkerEdgeTreatment(edgeRound))
            .build()
        return MaterialShapeDrawable(shapeModel).apply {
            setTint(tintColor)
            paintStyle = Paint.Style.FILL_AND_STROKE
            strokeWidth = strokeWidth1
            //strokeColor = ContextCompat.getColorStateList(context, R.color.red)
        }
    }

    //内边三角形(triangleEdgeInside=true) ,外边三角形(triangleEdgeInside=false)
    fun roundedCornerTriangle(round:Float,triangleEdge:Float,strokeWidth1:Float,tintColor:Int=Color.GRAY,triangleEdgeInside:Boolean=true): MaterialShapeDrawable {
        val shapeModel = ShapeAppearanceModel.builder()
            .setAllCorners(RoundedCornerTreatment())
            .setAllCornerSizes(round)
            .setAllEdges(TriangleEdgeTreatment(triangleEdge, triangleEdgeInside))
            .build()
        return MaterialShapeDrawable(shapeModel).apply {
            setTint(tintColor)
            paintStyle = Paint.Style.FILL_AND_STROKE
            strokeWidth = strokeWidth1
            //strokeColor = ContextCompat.getColorStateList(context, R.color.red)
        }
        //(textView03.parent as ViewGroup).clipChildren = false
    }

    //bubble bottom
    fun bubbleBottom(corner:Float,triangleEdge:Float,offsetEdgeTreatment:Float,tintColor:Int=Color.GRAY): MaterialShapeDrawable {
        val shapeModel = ShapeAppearanceModel.builder()
            .setAllCornerSizes(corner)
            .setBottomEdge(
                OffsetEdgeTreatment(
                    TriangleEdgeTreatment(triangleEdge, false),
                    offsetEdgeTreatment
                )
            )
            .build()
        val backgroundDrawable = MaterialShapeDrawable(shapeModel).apply {
            setTint(tintColor)
            paintStyle = Paint.Style.FILL
        }
        //(textView04.parent as ViewGroup).clipChildren = false
       return backgroundDrawable
    }

    //bubble bottom
    fun bubbleRight(corner:Float,triangleEdge:Float,offsetEdgeTreatment:Float,tintColor:Int=Color.GRAY): MaterialShapeDrawable {
        val shapeModel = ShapeAppearanceModel.builder()
            .setAllCornerSizes(corner)
            .setRightEdge(
                OffsetEdgeTreatment(
                    TriangleEdgeTreatment(triangleEdge, false),
                    offsetEdgeTreatment
                )
            )
            .build()
        val backgroundDrawable = MaterialShapeDrawable(shapeModel).apply {
            setTint(tintColor)
            paintStyle = Paint.Style.FILL
        }
        //(textView04.parent as ViewGroup).clipChildren = false
        return backgroundDrawable
    }

}

//绘制阴影
fun MaterialShapeDrawable.shadow(context: Context, color: Int, elevation1:Float){
    shadowCompatibilityMode = MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS
    initializeElevationOverlay(context)
    setShadowColor(color)
    elevation = elevation1
}

interface ShapeableScope{

    fun round(value:Float,tintColor:Int=Color.GRAY)

    fun circle()

    //切角
    fun cut(value:Float,tintColor:Int=Color.GRAY)

    //菱形
    fun rhombus(value:Float,tintColor:Int=Color.GRAY)

    fun roundedCornerTriangle(round:Float,triangleEdge:Float,strokeWidth1:Float=0f,tintColor:Int=Color.GRAY,triangleEdgeInside:Boolean=true)

    fun roundedCorner(round:Float,edgeRound:Float,strokeWidth1:Float,tintColor:Int=Color.GRAY)

    fun bubbleBottom(corner:Float,triangleEdge:Float,offsetEdgeTreatment:Float,tintColor:Int=Color.GRAY)

    fun bubbleRight(corner:Float,triangleEdge:Float,offsetEdgeTreatment:Float,tintColor:Int=Color.GRAY)

    //叶子
    fun leaf()

}

fun Shapeable.sampleShape(scopeConfig:ShapeableScope.()->Unit){
    val scope = object:ShapeableScope{
        override fun round(value: Float, tintColor: Int) {
            shapeAppearanceModel = ShapeAppearanceModel.builder()
                .setAllCorners(RoundedCornerTreatment())
                .setAllCornerSizes(value)
                .build()
        }

        override fun circle() {
            shapeAppearanceModel = ShapeAppearanceModel.builder()
                .setAllCorners(CutCornerTreatment())
                .setAllCornerSizes(RelativeCornerSize(0.5f))
                .build()

        }

        override fun cut(value: Float, tintColor: Int) {
            shapeAppearanceModel = ShapeAppearanceModel.builder()
                .setAllCorners(CutCornerTreatment())
                .setAllCornerSizes(value)
                .build()
        }

        override fun rhombus(value: Float, tintColor: Int) {
            shapeAppearanceModel = ShapeAppearanceModel.builder()
                .setAllCorners(CutCornerTreatment())
                .setAllCornerSizes(RelativeCornerSize(0.5f))
                .build()
        }

        override fun roundedCorner(
            round: Float,
            edgeRound: Float,
            strokeWidth1: Float,
            tintColor: Int
        ) {
            shapeAppearanceModel = ShapeAppearanceModel.builder()
                .setAllCorners(RoundedCornerTreatment())
                .setAllCornerSizes(round)
                .setAllEdges(MarkerEdgeTreatment(edgeRound))
                .build()
        }

        override fun leaf() {
            shapeAppearanceModel = ShapeAppearanceModel.builder()
                .setTopLeftCorner(CornerFamily.ROUNDED,RelativeCornerSize(0.5f))
                .setBottomRightCorner(CornerFamily.ROUNDED,RelativeCornerSize(0.5f))
                .build()
        }

        override fun roundedCornerTriangle(
            round: Float,
            triangleEdge: Float,
            strokeWidth1: Float,
            tintColor: Int,
            triangleEdgeInside: Boolean
        ) {
            shapeAppearanceModel = ShapeAppearanceModel.builder()
                .setAllCorners(RoundedCornerTreatment())
                .setAllCornerSizes(round)
                .setAllEdges(TriangleEdgeTreatment(triangleEdge, triangleEdgeInside))
                .build()
        }


        override fun bubbleBottom(
            corner: Float,
            triangleEdge: Float,
            offsetEdgeTreatment: Float,
            tintColor: Int
        ) {
            shapeAppearanceModel= ShapeAppearanceModel.builder()
                .setAllCornerSizes(corner)
                .setBottomEdge(
                    OffsetEdgeTreatment(
                        TriangleEdgeTreatment(triangleEdge, false),
                        offsetEdgeTreatment
                    )
                )
                .build()
        }

        override fun bubbleRight(
            corner: Float,
            triangleEdge: Float,
            offsetEdgeTreatment: Float,
            tintColor: Int
        ) {
            shapeAppearanceModel = ShapeAppearanceModel.builder()
                .setAllCornerSizes(corner)
                .setRightEdge(
                    OffsetEdgeTreatment(
                        TriangleEdgeTreatment(triangleEdge, false),
                        offsetEdgeTreatment
                    )
                )
                .build()
        }

    }
}